import 'package:country_picker/country_picker.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:znmm/UI/A_Common/custom_textField.dart';
import 'package:znmm/UI/utils/my_global.dart';
import 'package:znmm/config/app_color.dart';

import '../manage/cn_user_mag.dart';
import '../model/cn_user_model.dart';

class CnBillingEditView extends StatefulWidget {
  const CnBillingEditView({super.key});

  @override
  State<CnBillingEditView> createState() => _CnBillingEditViewState();
}

class _CnBillingEditViewState extends State<CnBillingEditView> {
  final CnUserModel _pModel = CnUserMag().getCnUserModel();
  List<String> _selectStrList = [];
  String _countryStr = "";

  @override
  void initState() {
    super.initState();

    _selectStrList = List.filled(
      _pModel.billingList.length,
      "",
    );

    for (int iIndex = 0; iIndex < _pModel.billingList.length; iIndex++) {
      String strKey = _pModel.billingKeyList[iIndex];
      String strText = _pModel.billingList[iIndex];

      if (strKey == "country") {
        _countryStr = strText;
      }
      _selectStrList[iIndex] = strText;
    }
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> itemList = [];

    for (int iIndex = 0; iIndex < _pModel.billingList.length; iIndex++) {
      String strKey = _pModel.billingKeyList[iIndex];
      String strText = _pModel.billingList[iIndex];

      if (strKey == "country") {
        itemList.add(GestureDetector(
          onTap: () {
            showCountryPicker(
              context: context,
              useSafeArea: true,
              showPhoneCode:
                  false, // optional. Shows phone code before the country name.
              onSelect: (Country country) {
                setState(() {
                  String strCountry = '${country.flagEmoji} ${country.name}';
                  print("strCountry:$strCountry");
                  _countryStr = strCountry;
                  _selectStrList[iIndex] = strCountry;
                  if (mounted) {
                    setState(() {});
                  }
                });
              },
            );
          },
          child: Container(
            height: 50,
            width: double.infinity,
            margin: const EdgeInsets.fromLTRB(20, 0, 20, 10),
            padding: const EdgeInsets.only(left: 5),
            alignment: Alignment.centerLeft,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(10.0), // 设置圆角半径为10
              border: Border.all(
                color: Colors.grey, // 设置边框颜色为黑色
                width: 0.5, // 设置边框宽度为1像素
              ),
            ),
            child: Text(
              textAlign: TextAlign.left,
              _countryStr,
              style: const TextStyle(fontSize: 16),
            ),
          ),
        ));
      } else {
        itemList.add(Padding(
          padding: const EdgeInsets.fromLTRB(20, 0, 20, 10),
          child: CustomTextField(
            onInputComplete: (value) {
              //print(value);
              _selectStrList[iIndex] = value;
            },
            labelText: strKey,
            strText: strText,
          ),
        ));
      }
    }

    return Scaffold(
        backgroundColor: AppColor.backgroundColor,
        appBar: MyGlobal.getNavigationBar(context, "Billing Info", actions: [
          TextButton(
            onPressed: () {
              CnUserMag().billingEdit(
                "billing",
                _pModel.billingKeyList,
                _selectStrList,
                callback: () {
                  Get.back(result: true);
                },
              );
            },
            child: const Text(
              "Save",
              style: TextStyle(
                fontSize: 17,
                color: Colors.black,
              ),
            ),
          )
        ]),
        body: Container(
            margin: const EdgeInsets.fromLTRB(0, 20, 0, 30),
            child: Scrollbar(
              child: SingleChildScrollView(
                child: Column(
                  children: itemList,
                ),
              ),
            )));
  }
}
